﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;

namespace BladeTecWebApplication.Admin
{
	public partial class ViewAttempts : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				NetSysData.NetSysData data = new NetSysData.NetSysData();
				GetAttemptList_Result[] attempts = data.GetAttemptList();

				if (attempts.Count() != 0)
				{
					gvAttempts.DataSource = attempts;
					gvAttempts.DataBind();
					pnlGrid.Visible = true;
					pnlNoAttempts.Visible = false;
				}
				else
				{
					pnlGrid.Visible = false;
					pnlNoAttempts.Visible = true;
				}

			}
		}

		protected void btnDelete_Click(object sender, EventArgs e)
		{
			int result;
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			foreach (string s in Request.Form)
			{
				if (s.IndexOf("attempt_") == 0)
				{
					int attempt_id = Int32.Parse(Request.Form.Get(s));
					result = data.DeleteAttempt(attempt_id);
				}
			}

			GetAttemptList_Result[] attempts = data.GetAttemptList();
			if (attempts.Count() != 0)
			{
				gvAttempts.DataSource = attempts;
				gvAttempts.DataBind();
				pnlGrid.Visible = true;
				pnlNoAttempts.Visible = false;
			}
			else
			{
				pnlGrid.Visible = false;
				pnlNoAttempts.Visible = true;
			}
		}

		protected void btnCompareApplicants_Click(object sender, EventArgs e)
		{
			string attempts = "";
			foreach (string s in Request.Form)
			{
				if (s.IndexOf("attempt_") == 0)
				{
					string attempt_id = Request.Form.Get(s);
					attempts += attempt_id + ":";
				}
			}

			if (attempts[attempts.Length - 1] == ':')
			{
				attempts = attempts.Substring(0, attempts.Length - 1);
			}

			Response.Redirect("CompareApplicants.aspx?attempt_ids=" + attempts);
		}

		public string getPercentage(double num, double den)
		{
			double pct = num / (num+den);
			pct = pct * 100;
			if (double.IsNaN(pct))
			{
				return "0";
			}
			return ((int)pct).ToString();
		}
	}
}